package com.bykj.property.api.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bykj.property.api.service.WxAppHouseService;
import com.bykj.property.api.vo.WxRentHouseSelectVo;
import com.bykj.property.api.vo.WxRentHouseVo;
import com.bykj.property.api.vo.WxSecHouseSelectVo;
import com.bykj.property.api.vo.WxSecHouseVo;
import com.bykj.property.model.*;
import com.bykj.property.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WxAppHouseServiceImpl implements WxAppHouseService {
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private RentHouseService rentHouseService;
    @Autowired
    private PmVillageService pmVillageService;
    @Autowired
    private RentHouseImageService rentHouseImageService;
    @Autowired
    private SechandRoomService sechandRoomService;
    @Autowired
    private  SechandRoomImageService sechandRoomImageService;
    @Value("${rent-area-label}")
    private String area;//字典表 地区对应label
    @Value("${rent-area-type}")
    private String area_type;//字典表 地区对应type

    @Value("${rent-price-label}")
    private String rent_price; //字典表 价格对应label  rent_price
    @Value("${rent-price-type}")
    private String price_type;//字典表 价格对应type

    @Value("${rent-type-label}")
    private String rent_type;//字典表 出租类型对应label
    @Value("${rent-type-type}")
    private String type_type;//字典表 出租类型对应type

    @Value("${sec-area-label}")
    private String sec_area_label;//字典表 二手房面积类型对应label
    @Value("${sec-area-type}")
    private String sec_area_type;//字典表 二手房面积类型对应type

    @Value("${sec-type-label}")
    private String sec_type_label;//字典表 二手房类型对应label
    @Value("${sec-type-type}")
    private String sec_type_type;//字典表 二手房类型对应type

    @Override
    public WxRentHouseSelectVo getInfoForSelect() {
        WxRentHouseSelectVo wxRentHouseSelectVo = new WxRentHouseSelectVo();
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("dictionary");
        Dictionary dictionaryarea = new Dictionary();
        dictionaryarea.setLabel(area);
        dictionaryarea.setType(area_type);
        queryWrapper.setEntity(dictionaryarea);
        List<Dictionary> dictionaryarealist = dictionaryService.list(queryWrapper);
        wxRentHouseSelectVo.setRentArea(dictionaryarealist);

        dictionaryarea.setLabel(rent_price);
        dictionaryarea.setType(price_type);
        queryWrapper.setEntity(dictionaryarea);
        List<Dictionary> dictionarypricelist = dictionaryService.list(queryWrapper);
        wxRentHouseSelectVo.setRentPrice(dictionarypricelist);

        dictionaryarea.setLabel(rent_type);
        dictionaryarea.setType(type_type);
        queryWrapper.setEntity(dictionaryarea);
        List<Dictionary> dictionarytypelist = dictionaryService.list(queryWrapper);
        wxRentHouseSelectVo.setRentType(dictionarytypelist);
        return wxRentHouseSelectVo;
    }

    @Override
    public List<WxRentHouseVo> getHouses(String requestAreaid, String price, String type) {
        Long areaId = null;
        String typeId = null;
        String pricestart = null;
        String priceend = null;
        if (StringUtils.isNotEmpty(requestAreaid)){
            Dictionary dictionaryArea = getDictionary(requestAreaid);
            areaId = dictionaryArea.getDictionary();
        }
        if (StringUtils.equals(type,"合租")){
            typeId = "2";
        }else if (StringUtils.equals(type,"整租")){
            typeId = "1";
        }
        if (StringUtils.isNotEmpty(price)){
            if (StringUtils.contains(price,"≥")) { // ">5000" "22-333"
                String[] strings = price.split("≥");
                priceend = strings[1];
            } else if (StringUtils.contains(price,"≤")){
                String[] strings = price.split("≤");
                pricestart = strings[1];
            } else if (StringUtils.contains(price,"≤")){
                String[] strings = price.split("~");
                priceend = strings[1];
                pricestart = strings[0];
            }
        }

        QueryWrapper<RentHouse> queryWrapper = new QueryWrapper<>();
        RentHouse rentHousequery = new RentHouse();
        if (areaId!=null && !areaId.toString().equals("23"))
            rentHousequery.setDictionaryId(Long.valueOf(areaId));
        if (typeId!=null)
            rentHousequery.setRentType(typeId);
        if (pricestart!=null && priceend==null)
            queryWrapper.le("rent_price",pricestart);
        if (pricestart==null && priceend!=null)
            queryWrapper.ge("rent_price",priceend);
        if (pricestart!=null && priceend!=null)
            queryWrapper.between("rent_price",pricestart,priceend);

        queryWrapper.setEntity(rentHousequery);

        List<WxRentHouseVo> rentHouseVos = new ArrayList<>();
        List<RentHouse> rentHouses = rentHouseService.list(queryWrapper);
        if (rentHouses!=null&&rentHouses.size()>0){
            for (RentHouse rentHouse:rentHouses) {
                WxRentHouseVo rentHouseVo = JSONObject.parseObject(JSON.toJSONString(rentHouse),WxRentHouseVo.class);
                if (rentHouseVo.getRentType().equals("1"))
                    rentHouseVo.setTypename("整租");
                if (rentHouseVo.getRentType().equals("2"))
                    rentHouseVo.setTypename("合租");
                PmVillage pmVillage = pmVillageService.getById(rentHouse.getPmVillageId());
                rentHouseVo.setVillagename(pmVillage!=null?pmVillage.getViName():null);

                RentHouseImage rentHouseImage = new RentHouseImage();
                rentHouseImage.setRentHouse(rentHouse.getRentHouseId());
                QueryWrapper<RentHouseImage> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.setEntity(rentHouseImage);
                List<RentHouseImage> rentHouseImageDB = rentHouseImageService.list(queryWrapper1);
                if (rentHouseImageDB!=null&&rentHouseImageDB.size()>0){
                    List<String> rentHouseImages = new ArrayList<>();
                    for (RentHouseImage rentHouseImageaa:rentHouseImageDB) {

                        rentHouseImages.add(rentHouseImageaa.getImageUrl());
                    }
                    rentHouseVo.setRentHouseImageList(rentHouseImages);
                }
                rentHouseVos.add(rentHouseVo);
            }
        }

        return rentHouseVos;
    }

    @Override
    public WxSecHouseSelectVo getSechandForSelect() {
        WxSecHouseSelectVo wxSecHouseSelectVo = new WxSecHouseSelectVo();
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("dictionary");
        Dictionary dictionaryarea = new Dictionary();
        dictionaryarea.setLabel(area);
        dictionaryarea.setType(area_type);
        queryWrapper.setEntity(dictionaryarea);
        List<Dictionary> dictionaryarealist = dictionaryService.list(queryWrapper);
        wxSecHouseSelectVo.setDistrict(dictionaryarealist);

        dictionaryarea.setLabel(sec_area_label);
        dictionaryarea.setType(sec_area_type);
        queryWrapper.setEntity(dictionaryarea);
        List<Dictionary> dictionarypricelist = dictionaryService.list(queryWrapper);
        List<Dictionary> dictionaries = new ArrayList<>();
        for (Dictionary dictionary:dictionarypricelist) {
            if (!dictionary.getValue().equals("不限"))
                dictionary.setValue(dictionary.getValue()+"㎡");
        }
        wxSecHouseSelectVo.setSecArea(dictionarypricelist);

        dictionaryarea.setLabel(sec_type_label);
        dictionaryarea.setType(sec_type_type);
        queryWrapper.setEntity(dictionaryarea);
        List<Dictionary> dictionarytypelist = dictionaryService.list(queryWrapper);
        wxSecHouseSelectVo.setSecType(dictionarytypelist);
        return wxSecHouseSelectVo;
    }

    @Override
    public List<WxSecHouseVo> getSecHandHouses(String district, String secArea, String secType) {
        Long areaId = null;
        String typeId = null;
        String areastart = null;
        String areaend = null;
        if (StringUtils.isNotEmpty(district)){
            Dictionary dictionaryArea = getDictionary(district);
            areaId = dictionaryArea.getDictionary();
        }
        if (StringUtils.equals(secType,"一室")){
            typeId = "1";
        }else if (StringUtils.equals(secType,"两室")){
            typeId = "2";
        }else if (StringUtils.equals(secType,"三室室")){
            typeId = "3";
        }else if (StringUtils.equals(secType,"四室及以上")){
            typeId = "4";
        }
        if (StringUtils.isNotEmpty(secArea)&&!secArea.equals("不限")){
            String secArea2 = secArea.replace("㎡","");
            if (StringUtils.contains(secArea2,"≥")) { // ">5000" "22-333"
                String[] strings = secArea2.split("≥");
                areaend = strings[1];
            } else if (StringUtils.contains(secArea2,"≤")){
                String[] strings = secArea2.split("≤");
                areastart = strings[1];
            } else if (StringUtils.contains(secArea2,"≤")){
                String[] strings = secArea2.split("~");
                areaend = strings[1];
                areastart = strings[0];
            }
        }
        QueryWrapper<SechandRoom> queryWrapper = new QueryWrapper<>();
        SechandRoom sechandRoom = new SechandRoom();
        if (areaId!=null && !areaId.toString().equals("23"))
            sechandRoom.setDictionaryId(Long.valueOf(areaId));
        if (typeId!=null)
            sechandRoom.setRoomType(secType);
        if (areastart!=null && areaend==null)
            queryWrapper.le("room_area",areastart);
        if (areastart==null && areaend!=null)
            queryWrapper.ge("room_area",areaend);
        if (areastart!=null && areaend!=null)
            queryWrapper.between("room_area",areastart,areaend);

        queryWrapper.setEntity(sechandRoom);
        List<WxSecHouseVo> wxSecHouseVoList = new ArrayList<>();
        List<SechandRoom> sechandRooms = sechandRoomService.list(queryWrapper);
        if (sechandRooms!=null && sechandRooms.size()>0){
            for (SechandRoom sechandroom: sechandRooms) {
                WxSecHouseVo wxSecHouseVo = JSONObject.parseObject(JSON.toJSONString(sechandroom),WxSecHouseVo.class);
                if (sechandroom.getRoomType().equals("1"))
                    wxSecHouseVo.setSectypename("一室");
                if (sechandroom.getRoomType().equals("2"))
                    wxSecHouseVo.setSectypename("两室");
                if (sechandroom.getRoomType().equals("3"))
                    wxSecHouseVo.setSectypename("三室");
                if (sechandroom.getRoomType().equals("4"))
                    wxSecHouseVo.setSectypename("四室及以上");
                PmVillage pmVillage = pmVillageService.getById(sechandroom.getPmVillageId());
                wxSecHouseVo.setVillagename(pmVillage!=null?pmVillage.getViName():null);

                SechandRoomImage sechandRoomImage = new SechandRoomImage();
                sechandRoomImage.setSechandRoom(sechandroom.getSechandRoomId());
                QueryWrapper<SechandRoomImage> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.setEntity(sechandRoomImage);
                List<SechandRoomImage> sechandRoomImageDB = sechandRoomImageService.list(queryWrapper1);
                if (sechandRoomImageDB!=null&&sechandRoomImageDB.size()>0){
                    List<String> rentHouseImages = new ArrayList<>();
                    for (SechandRoomImage sechandRoomImageaa:sechandRoomImageDB) {

                        rentHouseImages.add(sechandRoomImageaa.getImageUrl());
                    }
                    wxSecHouseVo.setSecHouseImageList(rentHouseImages);
                }
                wxSecHouseVoList.add(wxSecHouseVo);
            }

        }
        return wxSecHouseVoList;
    }

    public Dictionary getDictionary(String value) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        Dictionary dictionary = new Dictionary();
        dictionary.setValue(value);
        dictionary.setType("1");
        queryWrapper.setEntity(dictionary);
        return dictionaryService.getOne(queryWrapper);
    }
}
